// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class FadePanel extends cc.Component {


    public static get Instance(): FadePanel {
        return this.instance;
    }


    private static instance: FadePanel;

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        if (!FadePanel.instance) {
            FadePanel.instance = this;
            cc.game.addPersistRootNode(this.node.parent);
        }
    }

    start() {

    }

    // update (dt) {}

    FadeIn(call?: Function) {
        FadePanel.Instance.node.children[0].active = true;
        cc.tween(FadePanel.Instance.node.children[0])
            .to(1, { opacity: 255 })
            .call(() => {
                call && call();
            })
            .start();
    }

    FadeOut(call?: Function) {
        if (!FadePanel.Instance.node.children[0].active) return;
        cc.tween(FadePanel.Instance.node.children[0])
            .to(1, { opacity: 0 })
            .call(() => {
                call && call();
                FadePanel.Instance.node.children[0].active = false;
            })
            .start();
    }
}
